package com.mac.macjetpackdemo.view

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.widget.FrameLayout
import android.widget.LinearLayout

class Container : LinearLayout {
    private var row = 0
    private var column = 0
    private val TAG = "Container"
    private val rowViewList = mutableListOf<LinearLayout>()
    var mWidth = 0
    var mHeight = 0
    var mContext: Context? = null
    var mData = mutableListOf<ContainerBean>()
    var isInit: Boolean = false
    lateinit var mAdapter: MeetViewAdapter

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    ) {
        mContext = context
    }

    private fun initRowAndColumn() {
        when (mData.size) {
            1, 2 -> {
                row = 1
                column = mData.size
            }
            3, 4 -> {
                row = 2
                column = 2
            }
            5, 6 -> {
                row = 2
                column = 3
            }
            7, 8, 9 -> {
                row = 3
                column = 3
            }
            else -> {
                row = 3
                column = 3
            }
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = MeasureSpec.getSize(widthMeasureSpec)
        mHeight = MeasureSpec.getSize(heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        if (mData.size > 0 && !isInit) {
            layout(mContext)
            isInit = true
        }
    }

    private fun layout(context: Context?) {
        orientation = VERTICAL
        rowViewList.clear()
        for (i in 0 until row) {
            val linearLayout = LinearLayout(context)
            val param = LinearLayout.LayoutParams(mWidth, mHeight / row)
            linearLayout.gravity = Gravity.CENTER
            linearLayout.orientation = HORIZONTAL
            linearLayout.layoutParams = param
            addView(linearLayout)
            rowViewList.add(linearLayout)
        }

        for (j in 0 until rowViewList.size) {
            val it = rowViewList[j]
            for (i in 0 until column) {
                if (column * j + i >= mData.size) {
                    break
                }
                context?.apply {
                    val fl = FrameLayout(this)
                    val param = LinearLayout.LayoutParams(
                        mWidth / column,
                        LayoutParams.MATCH_PARENT
                    )
                    param.gravity = Gravity.CENTER
                    fl.layoutParams = param
                    fl.addView(mAdapter.getView(column * j + i))
                    it.addView(fl, i)
                }
            }
        }
    }

    private fun setData(data: MutableList<ContainerBean>) {
        removeAllViews()
        mData = data
        initRowAndColumn()
        if (mData.size > 0) {
            layout(mContext)
        }
        invalidate()
    }

    private val observer = object : MeetViewAdapter.AdapterDataObserver {
        override fun onChanged(data: MutableList<ContainerBean>) {
            setData(data)
        }
    }

    fun setAdapter(adapter: MeetViewAdapter) {
        mAdapter = adapter
        mAdapter.registerAdapterDataObserver(observer)
    }
}